console.log(faceapi.nets)
const video = document.getElementById("video")
console.log(video)
console.log("加载ai模型")
Promise.all([
        faceapi.nets.tinyFaceDetector.loadFromUri("/weights"),
        faceapi.nets.faceLandmark68Net.loadFromUri("/weights"),
        faceapi.nets.faceRecognitionNet.loadFromUri("/weights"),
        faceapi.nets.faceExpressionNet.loadFromUri("/weights")
    ]).then(startVideo)
    // edge浏览器无法获取摄像头
function startVideo() {
    var constraints = {
        video: "true",
        video: { width: 1280, height: 720 }
    };
    navigator.mediaDevices.getUserMedia(constraints)
        .then(function(mediaStream) {
            var video1 = document.querySelector('video');
            video1.srcObject = mediaStream;
            video1.onloadedmetadata = function(e) {
                video1.play();
            };
        }).catch(function(err) { console.log(err.name + ": " + err.message); }); // 总是在最后检查错误
}

video.addEventListener('play', () => {
    const canvas = faceapi.createCanvasFromMedia(video)
    document.body.append(canvas)
    const displaySize = { width: video.width, height: video.height }
    setInterval(async() => {
        const detections = await faceapi.detectAllFaces(video,
            new faceapi.TinyFaceDetectorOptions()).withFaceLandmarks().withFaceExpressions()
        console.log(detections)
        const resizedDetections = faceapi.resizeResults(detections, displaySize)
        canvas.getContext("2d").clearRect(0, 0, canvas.width, canvas.height)
        faceapi.draw.drawDetections(canvas, resizedDetections)
        faceapi.draw.drawFaceLandmarks(canvas, resizedDetections)
    }, 100)
})
startVideo()